package indi.kider.server.meter.api;

import indi.kider.common.model.bill.Meter;
import indi.kider.common.model.meter.CommandResponse;
import indi.kider.common.model.meter.InstantCommand;
import indi.kider.common.model.meter.MeterReading;
import indi.kider.server.meter.service.DayReadingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import pers.vankid.common.model.base.CommonResponse;

@RestController
public class MeterReadingController {
    @Autowired
    private DayReadingService dayReadingService;

    /**
     * 内部服务
     * 读日冻结数据
     *
     * @param meterReading
     * @return
     */
    @PostMapping("reading")
    public CommonResponse<MeterReading> getDayReading(@RequestBody MeterReading meterReading) {
        try {
            return CommonResponse.buildGetSuccessResponse(dayReadingService.getDayReading(meterReading));
        } catch (Exception e) {
            e.printStackTrace();
           return CommonResponse.buildBadRequestResponse(e.getMessage());
        }
    }

    /**
     * 抄水表
     * @param command
     * @return
     */
    @PostMapping("water/reading")
    public CommonResponse<MeterReading> readWaterMeter(@RequestBody InstantCommand command) {
        try {
            return CommonResponse.buildGetSuccessResponse(dayReadingService.readWaterMeter(command));
        } catch (Exception e) {
            return CommonResponse.buildExceptionResponse(e);
        }
    }

    /**
     * 抄电表
     * @param command
     * @return
     */
    @PostMapping("power/reading")
    public CommonResponse<MeterReading> readPowerMeter(@RequestBody InstantCommand command) {
        try {
            return CommonResponse.buildGetSuccessResponse(dayReadingService.readPowerMeter(command));
        } catch (Exception e) {
            return CommonResponse.buildExceptionResponse(e);
        }
    }

    @Value("${meter.command-model}")
    private boolean cmd ;
    /**
     * 发送命令（拉合闸）
     * @param command
     * @return
     */
    @PostMapping("command")
    public CommonResponse<CommandResponse> sendCommand(@RequestBody InstantCommand command) {
        if(cmd){
            try {
                return CommonResponse.buildDefaultSuccessResponse("操作成功",dayReadingService.sendCommand(command));
            } catch (Exception e) {
                return CommonResponse.buildExceptionResponse(e);
            }
        }else   return CommonResponse.buildBadRequestResponse("暂不开放该功能！");

    }

    /**
     * 获取表档案
     *
     * @param meter
     * @return
     */
    @PostMapping("document")
    public CommonResponse<Meter> getMeterDocument(@RequestBody Meter meter) {
        try {
            return CommonResponse.buildGetSuccessResponse("查询成功", dayReadingService.getMeterDocument(meter));
        } catch (Exception e) {
            return CommonResponse.buildExceptionResponse(e);
        }
    }
}
